package com.heima.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //检查参数
        if (dto == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //封装查询条件
        //分页条件
        Page pageReq = new Page(dto.getPage(), dto.getSize());
        //查询条件
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.<AdSensitive>lambdaQuery();

        queryWrapper.like(StringUtils.isNotBlank(dto.getName()),AdSensitive::getSensitives,dto.getName());
        //执行查询
        IPage<AdSensitive> pageResult = page(pageReq, queryWrapper);
        //封装pageResult
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal());

        responseResult.setData(pageResult.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //检查参数（敏感词不为空）
        if (adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives())) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"敏感词不能为空");
        }
        //检查敏感词在数据库中是否存在
        int count = count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count>0) {
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"敏感词已存在，请重新输入");
        }
        //保存敏感词
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        //返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //检查参数
        if (adSensitive == null || adSensitive.getId() == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"敏感词不能为空");
        }
        //基于id查询敏感词
        AdSensitive byId = getById(adSensitive.getId());
        if (byId == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词不存在");
        }
        //敏感词不为空，并且敏感词改变了，检查新的敏感词是否存在
        if (StringUtils.isNotBlank(adSensitive.getSensitives()) &&
                !adSensitive.getSensitives().equals(byId.getSensitives())) {
            int count = count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
            if (count>0) {
                CustException.cust(AppHttpCodeEnum.DATA_EXIST,"敏感词已存在 ");
            }
        }
        //修改敏感词信息
        updateById(adSensitive);
        //返回结果
        return ResponseResult.okResult();
    }
    @Override
    public ResponseResult delete(Integer id) {
        //检查id是否为空
        if (id == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词不存在");
        }
        //根据id删除敏感词
        removeById(id);
        //返回结果
        return ResponseResult.okResult();
    }
}
